import { getAgreement } from '@/api/user'

function privacyPreview(type = 'user', code = 'BASIC') {
  getAgreement({
    code: code,
    flag: ''
  }).then(res => {
    if (res.code === 0) {
      const webUrl = res.data[`${type}Agreement`] || ''
      uni.showLoading()
      uni.downloadFile({
        url: webUrl,
        success(res) {
          uni.openDocument({
            filePath: res.tempFilePath, //本地文档路径
            fileType: 'pdf', //文档类型
            showMenu: true,
            success: function (res) {
              uni.hideLoading()
            },
            fail: function (res) {
              uni.showToast({
                title: '打开文档失败'
              })
            }
          })
        },
        fail() {
          uni.hideLoading()
        }
      })
    } else {
      uni.showToast({
        title: res.msg,
        icon: 'none'
      })
    }
  })
}

export default privacyPreview
